{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# DC Line dispatch with pandapower OPF\n",
    "This is an introduction into the usage of the pandapower optimal power flow with dc lines.\n",
    "\n",
    "## Example Network\n",
    "\n",
    "We use the following four bus example network for this tutorial:\n",
    "\n",
    "<img src=\"pics/example_opf_dcline.png\" width=\"100%\">\n",
    "\n",
    "We first create this network in pandapower:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pandapower.create import (\n",
    "    create_empty_network,\n",
    "    create_bus,\n",
    "    create_lines,\n",
    "    create_dcline,\n",
    "    create_ext_grid,\n",
    "    create_load,\n",
    "    create_poly_cost\n",
    ")\n",
    "from pandapower.run import runpp, runopp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "net = create_empty_network()\n",
    "\n",
    "b1 = create_bus(net, 380)\n",
    "b2 = create_bus(net, 380)\n",
    "b3 = create_bus(net, 380)\n",
    "b4 = create_bus(net, 380)\n",
    "b5 = create_bus(net, 380)\n",
    "\n",
    "l1, l2, l3 = create_lines(net, [b1, b3, b4], [b2, b4, b5], [30, 20, 20], \"490-AL1/64-ST1A 380.0\")\n",
    "\n",
    "dcl1 = create_dcline(net, name=\"dc line\", from_bus=b2, to_bus=b3, p_mw=200, loss_percent=1.0,\n",
    "                  loss_mw=0.5, vm_from_pu=1.01, vm_to_pu=1.012, max_p_mw=1000,\n",
    "                  in_service=True)\n",
    "\n",
    "eg1 = create_ext_grid(net, b1, 1.02, min_p_mw=0.)\n",
    "eg2 = create_ext_grid(net, b5, 1.02, min_p_mw=0.)\n",
    "\n",
    "l1 = create_load(net, bus=b4, p_mw=800, controllable = False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "We now run a regular load flow to check out the DC line model:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "runpp(net)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The transmission power of the DC line is defined in the loadflow as given by the p_kw parameter, which was set to 200 MW:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "net.res_dcline"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The losses amount to 2.5 MW, which are made up of 0.5 MW conversion loss and 200 MW * 0.01 = 2 MW transmission losses. The voltage setpoints defined at from and to bus are complied with. \n",
    "\n",
    "Now lets define costs for the external grids to run an OPF:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "costeg0 = create_poly_cost(net, 0, 'ext_grid', cp1_eur_per_mw=10)\n",
    "costeg1 = create_poly_cost(net, 1, 'ext_grid', cp1_eur_per_mw=8)\n",
    "net.bus['max_vm_pu'] = 1.5\n",
    "net.line['max_loading_percent'] = 1000"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "runopp(net, delta=1e-16)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This function runs an Optimal Power Flow using the PYPOWER OPF. To make sure that the PYPOWER OPF converges, we decrease the power tolerance `delta` (the default value is `delta=1e-10`). The power tolerance `delta` is a measure of the extent to which exceeding of minimum and maximum power limits is tolerated. That is, in above case, the limits considered by the OPF for the generators are `min_p_mw - delta` and `max_p_mw + delta` as lower and upper bound respectively on the active power. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Since we defined lower costs for Ext Grid 2, it fully services the load:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "net.res_ext_grid"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "While the DC line does not transmit any power:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "net.res_dcline"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If we set the costs of the left grid to a lower value than the right grid and run the loadflow again:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "net.poly_cost.cp1_eur_per_mw.at[costeg0] = 8\n",
    "net.poly_cost.cp1_eur_per_mw.at[costeg1] = 10\n",
    "runopp(net, delta=1e-16)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can see that the power now comes from the left ext_grid:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "net.res_ext_grid"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And is transmitted over the DC line:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "net.res_dcline"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can however see that the lines on the left hand side are now overloaded:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "net.res_line.loading_percent"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If we set the maximum line loading to 100% and run the OPF again:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "net.line[\"max_loading_percent\"] = 100\n",
    "runopp(net, delta=1e-16)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can see that the lines are no longer overloaded:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "net.res_line.loading_percent"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Because the load is serviced from both grids:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "net.res_ext_grid"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And the DC line transmits only part of the power needed to service the load:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "net.res_dcline"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Finally, we can also define transmission costs for the DC line:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "costeg1 = create_poly_cost(net, 0, 'dcline', cp1_eur_per_mw=3)\n",
    "runopp(net, delta=1e-16)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Because the sum of the costs for generating power on the left hand side (8) and transmitting it to the right side (3) is now larger than for generating on the right side (10), the OPF draws as much power from the right side as is possible without violating line loading constraints:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "net.res_line.loading_percent"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "net.res_dcline"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If we relax the line loading constraint and run the OPF again:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "net.line[\"max_loading_percent\"] = 1000\n",
    "runopp(net, delta=1e-16)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The load is once again fully serviced by the grid on the right hand side:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "net.res_ext_grid"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And the DC line is in open loop operation:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "net.res_dcline"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Little consistency check:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "net.res_ext_grid.p_mw.at[0]*8 + net.res_ext_grid.p_mw.at[1]*10 + net.res_dcline.p_from_mw.at[0]*3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "net.res_cost"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
